<template>
    <el-dialog v-model="visible" :title="dialogTitle" width="769px">
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="90px">
            <el-form-item label="角色名称" prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入账户名称" clearable />
            </el-form-item>
            <el-form-item label="角色编码" prop="code">
                <el-input v-model="ruleForm.code" placeholder="请输入账户名称" clearable />
            </el-form-item>
            <el-form-item label="数据权限" prop="dataScope">
                <el-select v-model="ruleForm.dataScope">
                    <el-option :key="0" label="全部数据" :value="0" />
                    <el-option :key="1" label="部门及子部门数据" :value="1" />
                    <el-option :key="2" label="本部门数据" :value="2" />
                    <el-option :key="3" label="本人数据" :value="3" />
                </el-select>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-switch v-model="ruleForm.status" inline-prompt active-text="正常" inactive-text="禁用" :active-value="1" :inactive-value="0" />
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number v-model="ruleForm.sort" :min="1" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button size="default" @click="onCancel">取 消</el-button>
                <el-button type="primary" size="default" @click="onSubmit(ruleFormRef)">保 存</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { Role } from '@/api/system/role/types.ts'
import { saveRole, updateRole } from '@/api/system/role/index.ts'
import { resetForm } from '@/utils/index.ts'

const visible = ref(false)
const dialogTitle = ref('新增角色')
const defaultRuleForm: Role.Form = {
    status: 1,
    sort: 1,
}
const ruleForm = reactive<Role.Form>({
    status: 1,
    sort: 1,
})
/** 定义表单校验规则 */
const rules = reactive<FormRules<Role.Form>>({
    name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
    code: [{ required: true, message: '请输入角色编码', trigger: 'blur' }],
    dataScope: [{ required: true, message: '请选择数据权限', trigger: 'change' }],
})

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh'])
/** 关闭弹窗 */
const onCancel = () => {
    visible.value = false
}
/** 保存 */
const ruleFormRef = ref<FormInstance>()
const onSubmit = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid) => {
        if (valid) {
            if (ruleForm.id) {
                updateRole(ruleForm)
                    .then(() => {
                        ElMessage.success('保存成功')
                        visible.value = false
                        emit('refresh')
                    })
                    .catch(() => {
                        ElMessage.error('保存失败')
                    })
            } else {
                saveRole(ruleForm)
                    .then(() => {
                        ElMessage.success('保存成功')
                        visible.value = false
                        emit('refresh')
                    })
                    .catch(() => {
                        ElMessage.error('保存失败')
                    })
            }
        }
    })
}

/** 打开弹窗 */
const openDialog = (item: Role.Form | undefined) => {
    ruleFormRef.value?.clearValidate()
    if (item) {
        dialogTitle.value = '编辑角色'
        Object.assign(ruleForm, item)
    } else {
        dialogTitle.value = '新增角色'
        resetForm(ruleForm, defaultRuleForm)
    }
    visible.value = true
}

defineExpose({
    openDialog,
})
</script>
